<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use App\Models\Member;

class MemberController extends Controller
{
    //用户管理
    public function index(){

    $data = DB::table('user')->select('user.*','wallet.money')
                              ->leftJoin('wallet', 'user.id', '=', 'wallet.uid')
                              ->orderBy('user.id', 'desc')
                              ->paginate(15);
        foreach ($data as $value) {
          $total = DB::table('tixian')->where([['openid',$value->open_id],['status',1]])->sum('price');
            $value->total = sprintf("%.2f", $total);
        }
      // $info = DB::table('tixian')->where([['openid','']['status',1]])->sum('price');
    	return view('admin.user.index',compact('data'));

    }


//编辑视图
    public function edit($uid){
$data = DB::table('user')->select('user.*','wallet.money')
                            ->where('user.id',$uid)
                          ->leftJoin('wallet', 'user.id', '=', 'wallet.uid')
                          ->first();
  return view('admin.user.edit',compact('data'));
}



//更新提交

    public function update(Request $request,$uid){

      $res = DB::table('wallet')->where('uid',$uid)->update([
            'money' => $request->money
        ]);
      if ($res == 1) {
         return redirect('admin/member');
      }else{
        return back()->with('更新失败！');
      }
    }

//搜索提交
    public function store(Request $request){
          $search = $request->keyword;
          $type = $request->stype;
          if ($type == 0) {
            $data = DB::table('user')->select('user.*','wallet.money')
                              ->leftJoin('wallet', 'user.id', '=', 'wallet.uid')
                              ->where('user.username', 'like', '%'.$search.'%')
                              ->orderBy('user.id', 'desc')
                              ->paginate(15);
          }else{
            $data = DB::table('user')->select('user.*','wallet.money')
                              ->leftJoin('wallet', 'user.id', '=', 'wallet.uid')
                              ->where('user.id', 'like', '%'.$search.'%')
                              ->orderBy('user.id', 'desc')
                              ->paginate(15);
          }
          return view('admin.user.index',compact('data'));
    }

    public function memberselect(Request $request){
           $data = DB::table('user')->select('user.*','wallet.money')
                              ->leftJoin('wallet', 'user.id', '=', 'wallet.uid')
                              ->where('user.open_id',$request->openid)
                              ->paginate(15);

        foreach ($data as $value) {
          $total = DB::table('tixian')->where([['openid',$value->open_id],['status',1]])->sum('price');
            $value->total = sprintf("%.2f", $total);
        }

       // $info = DB::table('tixian')->where([['openid','']['status',1]])->sum('price');
        return view('admin.user.index',compact('data'));
    }

}
